<template>
  <el-dialog
    :title="!dataForm.userid ? '添加会员' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="用户名" prop="username">
      <el-input v-model="dataForm.username" placeholder="请输入用户名"></el-input>
    </el-form-item>
      <el-form-item label="密码" prop="password" >
      <el-input type="password" v-model="dataForm.password" placeholder="请输入密码" autocomplete="off"></el-input>
    </el-form-item>
      <el-form-item label="确认密码" prop="checkPass">
      <el-input type="password" v-model="dataForm.checkPass" placeholder="请确认密码" autocomplete="off"></el-input>
    </el-form-item>
    <el-form-item label="昵称" prop="nickname">
      <el-input v-model="dataForm.nickname" placeholder="请输入昵称"></el-input>
    </el-form-item>
    <el-form-item label="邮箱" prop="email">
      <el-input v-model="dataForm.email" placeholder="请输入邮箱"></el-input>
    </el-form-item><el-form-item label="手机号" prop="mobile">
      <el-input v-model="dataForm.mobile" placeholder="请输入手机号"></el-input>
    </el-form-item>
    <el-form-item label="积分点数" prop="point">
      <el-input v-model="dataForm.point" placeholder="请输入积分点数"></el-input>
    </el-form-item>
      <el-form-item label="会员组" prop="vip">
      <el-select v-model="dataForm.vip" placeholder="会员组">
        <el-option
          v-for="item in options"
          :key="item.value"
          :label="item.label"
          :value="item.value">
        </el-option>
      </el-select>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      var validatePass = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入密码'));
        } else {
          if (this.dataForm.checkPass !== '') {
            this.$refs.dataForm.validateField('checkPass');
          }
          callback();
        }
      };
      var validatePass2 = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请再次输入密码'));
        } else if (value !== this.dataForm.password) {
          callback(new Error('两次输入密码不一致!'));
        } else {
          callback();
        }
      };
      return {
        value: '',
        visible: false,
        options: [{
          value: 0,
          label: '普通会员'
        }, {
          value: 1,
          label: '青铜会员'
        }, {
          value: 2,
          label: '白银会员'
        }, {
          value: 3,
          label: '黄金会员'
        }, {
          value: 4,
          label: '铂金会员'
        }, {
          value: 5,
          label: '钻石会员'
        }, {
          value: 6,
          label: '终生会员'
        }],
        dataForm: {
          userid: 0,
          phpssouid: '',
          username: '',
          password: '',
          checkPass: '',
          encrypt: '',
          nickname: '',
          headimg: '',
          regdate: '',
          lastdate: '',
          regip: '',
          lastip: '',
          loginnum: '',
          email: '',
          emailCode: '',
          groupid: '',
          areaid: '',
          amount: '',
          point: '',
          kuaibi: '',
          modelid: '',
          message: '',
          islock: '',
          vip: '',
          overduedate: '',
          siteid: '',
          connectid: '',
          unionid: '',
          from1: '',
          mobile: '',
          fuid: '',
          lahei: '',
          fenghao: '',
          inviteCode: '',
          brokerage: '',
          defaultKefu: '',
          freeCard: '',
          vipExpQuarter: '',
          vipExpTotal: '',
          vipStartTime: '',
          vipEndTime: '',
          kefuId: ''
        },
        dataRule: {
          username: [
            { required: true, message: '请输入用户名', trigger: 'blur' }
          ],
          password: [
            { validator: validatePass, trigger: 'blur' }
          ],
          checkPass: [
            { validator: validatePass2, trigger: 'blur' }
          ],
          email: [
            { required: true, message: '请输入邮箱', trigger: 'blur' },
            { max: 50, message: '邮箱在50字以内', trigger: 'blur' },
            { pattern: /^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.[a-zA-Z0-9]{2,6}$/, message: '请检查邮箱格式是否正确', trigger: 'blur' }
          ],
          mobile: [
            { required: true, message: '请输入手机号码', trigger: 'blur' },
            { pattern: /^((1[3,5,8][0-9])|(14[5,7])|(17[0,5,6,7,8])|(19[7]))\d{8}$/, message: '请检查手机号是否正确', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            alert('submit!');
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      init (id) {
        this.dataForm.userid = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.userid) {
            this.$http({
              url: this.$http.adornUrl(`/member/v9member/info/${this.dataForm.userid}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.phpssouid = data.v9Member.phpssouid
                this.dataForm.username = data.v9Member.username
                this.dataForm.password = data.v9Member.password
                this.dataForm.encrypt = data.v9Member.encrypt
                this.dataForm.nickname = data.v9Member.nickname
                this.dataForm.headimg = data.v9Member.headimg
                this.dataForm.regdate = data.v9Member.regdate
                this.dataForm.lastdate = data.v9Member.lastdate
                this.dataForm.regip = data.v9Member.regip
                this.dataForm.lastip = data.v9Member.lastip
                this.dataForm.loginnum = data.v9Member.loginnum
                this.dataForm.email = data.v9Member.email
                this.dataForm.emailCode = data.v9Member.emailCode
                this.dataForm.groupid = data.v9Member.groupid
                this.dataForm.areaid = data.v9Member.areaid
                this.dataForm.amount = data.v9Member.amount
                this.dataForm.point = data.v9Member.point
                this.dataForm.kuaibi = data.v9Member.kuaibi
                this.dataForm.modelid = data.v9Member.modelid
                this.dataForm.message = data.v9Member.message
                this.dataForm.islock = data.v9Member.islock
                this.dataForm.vip = data.v9Member.vip
                this.dataForm.overduedate = data.v9Member.overduedate
                this.dataForm.siteid = data.v9Member.siteid
                this.dataForm.connectid = data.v9Member.connectid
                this.dataForm.unionid = data.v9Member.unionid
                this.dataForm.from1 = data.v9Member.from1
                this.dataForm.mobile = data.v9Member.mobile
                this.dataForm.fuid = data.v9Member.fuid
                this.dataForm.lahei = data.v9Member.lahei
                this.dataForm.fenghao = data.v9Member.fenghao
                this.dataForm.inviteCode = data.v9Member.inviteCode
                this.dataForm.brokerage = data.v9Member.brokerage
                this.dataForm.defaultKefu = data.v9Member.defaultKefu
                this.dataForm.freeCard = data.v9Member.freeCard
                this.dataForm.vipExpQuarter = data.v9Member.vipExpQuarter
                this.dataForm.vipExpTotal = data.v9Member.vipExpTotal
                this.dataForm.vipStartTime = data.v9Member.vipStartTime
                this.dataForm.vipEndTime = data.v9Member.vipEndTime
                this.dataForm.kefuId = data.v9Member.kefuId
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/member/v9member/${!this.dataForm.userid ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'userid': this.dataForm.userid || undefined,
                'phpssouid': this.dataForm.phpssouid,
                'username': this.dataForm.username,
                'password': this.dataForm.password,
                'encrypt': this.dataForm.encrypt,
                'nickname': this.dataForm.nickname,
                'headimg': this.dataForm.headimg,
                'regdate': this.dataForm.regdate,
                'lastdate': this.dataForm.lastdate,
                'regip': this.dataForm.regip,
                'lastip': this.dataForm.lastip,
                'loginnum': this.dataForm.loginnum,
                'email': this.dataForm.email,
                'emailCode': this.dataForm.emailCode,
                'groupid': this.dataForm.groupid,
                'areaid': this.dataForm.areaid,
                'amount': this.dataForm.amount,
                'point': this.dataForm.point,
                'kuaibi': this.dataForm.kuaibi,
                'modelid': this.dataForm.modelid,
                'message': this.dataForm.message,
                'islock': this.dataForm.islock,
                'vip': this.dataForm.vip,
                'overduedate': this.dataForm.overduedate,
                'siteid': this.dataForm.siteid,
                'connectid': this.dataForm.connectid,
                'unionid': this.dataForm.unionid,
                'from1': this.dataForm.from1,
                'mobile': this.dataForm.mobile,
                'fuid': this.dataForm.fuid,
                'lahei': this.dataForm.lahei,
                'fenghao': this.dataForm.fenghao,
                'inviteCode': this.dataForm.inviteCode,
                'brokerage': this.dataForm.brokerage,
                'defaultKefu': this.dataForm.defaultKefu,
                'freeCard': this.dataForm.freeCard,
                'vipExpQuarter': this.dataForm.vipExpQuarter,
                'vipExpTotal': this.dataForm.vipExpTotal,
                'vipStartTime': this.dataForm.vipStartTime,
                'vipEndTime': this.dataForm.vipEndTime,
                'kefuId': this.dataForm.kefuId
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
